package com.lsk.mobile;

import com.alipay.api.AlipayApiException;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.upload.UploadFile;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.api.SnsApi;
import com.jfinal.weixin.sdk.kit.PaymentKit;
import com.lsk.api.YJFService;
import com.lsk.common.interceptor.CacheInterceptor;
import com.lsk.common.interceptor.MobileAuthorizationInterceptor;
import com.lsk.util.*;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/17 0017.
 */
@Before(MobileAuthorizationInterceptor.class)
public class MobileController extends Controller {
    Cache cache = Redis.use();
    static MobileServices mobileServices = new MobileServices();

    public void authorize() {
        String para = getPara();
        String orgId = getPara("orgId");
        String parentId = getPara("parentId");
        Record record = mobileServices.getWxConfigByOrgId(orgId, 0);
        String redirectUrl = record.getStr("domain");
        String appId = record.getStr("appId");
        String url = SnsAccessTokenApi.getAuthorizeURL(appId, redirectUrl + "/mobile/authorizeCallBack", para, false);
        Record member = new Record();//会员的临时信息
        Syslog.info("跳转的路径为   " + url);
        member.set("orgId", orgId).set("parentId", parentId);
        cache.setex(para, 60 * 10, member);
        redirect(url);
    }

    public void authorizeCallBack() {
        HttpSession session = getSession();
        String code = getPara("code");
        String state = getPara("state");
        Record member = cache.get(state);
        String orgId = member.getStr("orgId");
        String parentId = member.getStr("parentId");
        Record wxConfig = mobileServices.getWxConfigByOrgId(orgId, 0);
        String appId = wxConfig.getStr("appId");
        String appsecret = wxConfig.getStr("appsecret");
        SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appId, appsecret, code);
        System.out.println(snsAccessToken.getJson());
        String access_token = snsAccessToken.getAccessToken();
        String openId = snsAccessToken.getOpenid();
        ApiResult apiResult = SnsApi.getUserInfo(access_token, openId);
        String unionId = apiResult.get("unionid");
        String heardUrl = apiResult.get("headimgurl");
        String nickname = apiResult.get("nickname");
        member.set("openId", openId);
        member.set("unionId", unionId);
        mobileServices.saveMember(openId, unionId, orgId, nickname, heardUrl, parentId);
        cache.setex(openId, 60 * 60 * 24, member);
        session.setAttribute("openId", openId);
        redirect("/mobile/index");
    }

    /**
     * 去首页
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void index() {
        String type = getCookie("type");
        type = type == null || "".equals(type) ? "1" : type;
        ReturnData returnData = mobileServices.indexDataService(type);
        setAttr("indexData", returnData.getData());
        renderJsp("index.jsp");
    }

    /**
     * 首页数据
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void indexData() {

        String type = getPara("type");

        ReturnData returnData = mobileServices.indexDataService(type);

        renderJson(returnData);

    }

    /**
     * 分类数据
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void classification() {


        String cokeType = getCookie("type");

        String type = cokeType == null ? "1" : cokeType;

        String commodityClassId = getPara("commodityClassId");
        String index = getPara("myIndex");
        /*所有的分类*/
        List<Record> classList = mobileServices.getAllCommodityClassService("");
        List<Record> commodityList = null;

        if (classList.size() > 0) {

            String classId = null;
            if (commodityClassId != null && !"".equals(commodityClassId)) {
                classId = commodityClassId;
            } else {
                classId = classList.get(0).getStr("id");
            }

            commodityList = mobileServices.getCommodityByClassIdService(classId, type);
        }
        setAttr("commodityClassId", commodityClassId);
        setAttr("classList", classList);
        setAttr("commodityList", commodityList);
        setAttr("myIndex", index);
        renderJsp("classification.jsp");
    }

    /**
     * 分类数据
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void classificationData() {
        String type = getPara("type");
        /*所有的分类*/
        List<Record> classList = mobileServices.getAllCommodityClassService(type);
        List<Record> commodityList = null;
        if (classList.size() > 0) {
            String classId = classList.get(0).getStr("id");
            commodityList = mobileServices.getCommodityByClassIdService(classId, type);
        }
        Map map = new HashMap();
        map.put("classList", classList);
        map.put("commodityList", commodityList);
        ReturnData returnData = new ReturnData(true);
        returnData.setData(map);
        renderJson(returnData);
    }

    /**
     * 收卡页面
     */
    public void sellCard() {

        String type = getPara("type");//0 从卡类过来  1 从商品过来

        String token = getHeader("token");

        token = token == null ? getCookie("token") : token;

        String cardType = getCookie("type");

        cardType = cardType == null ? "1" : cardType;

        Map<String, Record> map = cache.get(token);

        Record memberInfo = map.get("memberInfo");

        String realName = memberInfo.getStr("realName");

        String alipayAccount = memberInfo.getStr("alipayAccount");

        String commodityClassId = getPara("commodityClassId");

        String commodityId = getPara("commodityId");


        Record commodity = mobileServices.getCommodityById(commodityId);//商品详情

        Record commodityClass = mobileServices.getClassInfoById(commodityClassId);

        String commodityClassName = getPara("commodityClassName");

        String commodityName = getPara("commodityName");

        List<Record> classList = mobileServices.getAllCommodityClassService("");

        List<Record> commodityList = mobileServices.getCommodityByClassIdService(commodityClassId, cardType);

        List<Record> faceValueList = mobileServices.getFaceValueService(commodityId);

        setAttr("commodity", commodity);

        setAttr("commodityClass", commodityClass);

        setAttr("faceValueList", faceValueList);

        setAttr("commodityId", commodityId);

        setAttr("commodityClassId", commodityClassId);

        setAttr("commodityClassName", commodityClassName);

        setAttr("commodityList", commodityList);

        setAttr("type", type);

        setAttr("commodityName", commodityName);

        setAttr("classList", classList);
        setAttr("realName", realName);
        setAttr("alipayAccount", alipayAccount);
        //如果是从卡类型过来
        if (commodity != null) {
            setAttr("hasCardNumber2", commodity.getStr("hasCardNumber"));
            setAttr("cardNumberSize2",commodity.getStr("cardNumberSize"));
            setAttr("cardPasswordSize2",commodity.getStr("cardPasswordSize"));
        }
        //当前端类型
        String cookeType = getCookie("type");
        //renderJsp("../mobileApply/sell_crad.jsp");

        if ("3".equals(cookeType)) {
            renderJsp("../mobileApply/sell_crad.jsp");
        } else {
            renderJsp("sell_crad.jsp");
        }


    }

    /**
     * 保存收卡
     */
    @Before(Tx.class)
    public void saveCard() {

        String token = getHeader("token");

        token = token == null ? getCookie("token") : token;


        Map<String, String[]> map = getParaMap();

        Map<String, Record> cacheMap = cache.get(token);

        Record memberInfo = cacheMap.get("memberInfo");


        cacheMap.remove("memberInfo");

        memberInfo.remove("alipayAccount");

        memberInfo.remove("realName");


        memberInfo.set("alipayAccount", map.get("alipayAccount")[0]);

        memberInfo.set("realName", map.get("realName")[0]);

        cacheMap.put("memberInfo", memberInfo);

        cache.setex(token, 60 * 60, cacheMap);

        ReturnData returnData = mobileServices.saveCardService(map, cacheMap);
        //ReturnData returnData = mobileServices.batchCardSettleService(map);

        renderJson(returnData);
    }

    /**
     *卡项整理
     */
    @Before(Tx.class)
    public void batchCardSettle(){


    }

    /**
     * 读取图片中的卡号以及密码
     */
    @Clear
    public void readImgText(){


        UploadFile file=getFile();

        String commodityId=getPara("commodityId");

        ReturnData returnData= mobileServices.readImgText(file.getFile().getPath(),commodityId);

        renderJson(returnData);
    }


    /**
     * 通过卡类型获取卡列表
     */
    public void cardList() {

        String type = getCookie("type");

        type = type == null ? "1" : type;

        String commodityClassId = getPara("commodityClassId");

        List<Record> commodtiyList = mobileServices.getCommodityByClassIdService(commodityClassId, type);

        setAttr("commodtiyList", commodtiyList);

        renderJsp("cardList.jsp");
    }

    /**
     * 通过卡类型获取卡列表(json形式)
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void cardListData() {

        String type = getPara("type");
        String commodityClassId = getPara("commodityClassId");
        List<Record> commodtiyList = mobileServices.getCommodityByClassIdService(commodityClassId, type);
        ReturnData returnData = new ReturnData(true);
        returnData.setData(commodtiyList);
        renderJson(returnData);

    }

    /**
     * 通过卡id获取面值
     */
    public void getFaceVlaue() {
        String commodityId = getPara("commodityId");
        List<Record> faceValueList = mobileServices.getFaceValueService(commodityId);
        setAttr("faceValueList", faceValueList);

        renderJsp("faceValue.jsp");
    }

    /**
     * 查找面值对应的平台商品有无卡号
     */
    public void hasCardNumber() {
        String feedbackId = getPara("feedbackId");
        ReturnData rd = mobileServices.hasCardNumberByFeedBackId(feedbackId);
        renderJson(rd);
    }

    /**
     * 通过卡id获取面值(Json形式)
     */
    @Clear
    @Before({CacheInterceptor.class})
    public void getFaceVlaueData() {
        String commodityId = getPara("commodityId");
        List<Record> faceValueList = mobileServices.getFaceValueService(commodityId);
        ReturnData returnData = new ReturnData(true);
        returnData.setData(faceValueList);
        renderJson(returnData);
    }

    /**
     * 获取收货地址
     */
    public void getMemberAddress() {
        HttpSession session = getSession();
        String openId = session.getAttribute("openId").toString();
        Record record = cache.get(openId);

        String orgId = record.getStr("orgId");
        String unionId = record.getStr("unionId");
        ReturnData returnData = mobileServices.getMemberAddressService(unionId, orgId);
        renderJson(returnData);
    }


    /**
     * 发起支付
     */
    public void pay() {
        HttpSession session = getSession();
        String openId = session.getAttribute("openId").toString();
        Record record = cache.get(openId);
        String orgId = record.getStr("orgId");
        String unionId = record.getStr("unionId");
        String commodityId = getPara("commodityId");
        int number = getParaToInt("number");
        ReturnData returnData = mobileServices.payService(openId, orgId, unionId, commodityId, number);
        setAttr("returnData", returnData);
        renderJsp("pay.jsp");
    }

    /**
     * 获取公告数据list,接口
     */
    public void noticesList() {
        String type=getPara("type");
        List list = mobileServices.getNoticeService(type);
        ReturnData rd = new ReturnData();
        rd.setCode("000000");
        rd.setMsg("获取成功");
        rd.setResult(true);
        rd.setData(list);
        renderJson(rd);

    }

    /**
     * 根据id查公告,接口
     */
    public void getNoticeById() {
        String id = getPara("id");
        ReturnData rd = new ReturnData();
        Record record = mobileServices.getNoticeById(id);
        rd.setCode("000000");
        rd.setMsg("获取成功");
        rd.setResult(true);
        rd.setData(record);
        renderJson(rd);
    }

    /**
     * 转到公告列表页面
     */
    public void toNoticeListPage() {
        String type=getPara("type");
        List list = mobileServices.getNoticeService(type);
        setAttr("notice", list);
        renderJsp("grzxAccountcement.jsp");
    }

    /**
     * 根据id获取notice详情并跳转页面
     */
    public void noticeDetails() {
        String id = getPara("id");
        Record record = mobileServices.getNoticeById(id);
        setAttr("notice", record);
        renderJsp("grzxAccountcementDetail.jsp");
    }

    /**
     * 去交易记录页面,分页
     */

    public void toTradeRecordPage() {

        String limitStr = getPara("limit");

        String pageStr = getPara("page");

        String token = getCookie("token");

        Map<String, Record> map = cache.get(token);

        Record record = map.get("memberInfo");


        limitStr = (limitStr == null ? "4" : limitStr);

        pageStr = (pageStr == null ? "1" : pageStr);

        Integer limit = Integer.valueOf(limitStr);
        Integer page = Integer.valueOf(pageStr);


        PageUtil pageUtil = mobileServices.getTradeRecordList(limit, page, record, null,"","","","");

        setAttr("data", pageUtil);

        renderJsp("testFlow.jsp");
    }

    /**
     * 查询交易记录的数据接口
     */

    public void getOrderCardList() {
        String token = getHeader("token");
        Map<String, Record> map = cache.get(token);

        String cardState = getPara("cardState");

        Integer limit = getParaToInt("limit");
        Integer page = getParaToInt("page");
        limit = (limit == null ? 4 : limit);
        page = (page == null ? 1 : page);
        String startDate = getPara("startDate");
        String endDate = getPara("endDate");

        String date=getPara("date");

        if (date!=null && "".equals(date)){

            startDate=date;

            endDate=date;

        }

        String cardNum=getPara("cardNum");
        String orderNum=getPara("orderNum");
        PageUtil pageUtil = mobileServices.getTradeRecordList(limit, page, map.get("memberInfo"), cardState,startDate,endDate,cardNum,orderNum);
        ReturnData rd = new ReturnData();
        rd.setData(pageUtil);
        rd.setResult(true);
        rd.setMsg("查询成功");
        rd.setCode("111111");
        renderJson(rd);
    }

    /**
     * 流加载交易查询的数据接口
     */
    public void getOrderCardFlowList() {

        String token = getHeader("token");

        if (token == null) {
            token = getCookie("token");
        }

        String cardState = getPara("cardState");    //0交易成功 1 交易失败 2 处理中 3 已打款

        String date=getPara("date");

        Map<String, Record> map = cache.get(token);
        Syslog.info("token:"+token);
        Syslog.info("map:"+map.toString());
        Integer page = getParaToInt("page");
        Integer limit = 4;
        page = (page == null ? 1 : page);



        PageUtil pageUtil = mobileServices.getTradeRecordList(limit, page, map.get("memberInfo"), cardState,"",date,"","");
        Map<String, Object> res = new HashMap<>();
        res.put("data", pageUtil.getList());
        int pages = (pageUtil.getTotalRows() / 4) + 1;
        res.put("pages", pages);
        renderJson(res);
    }

    @Clear
    @Before(CacheInterceptor.class)
    public void callBack() {
        String message = "";
        try {
            String xmlMsg = HttpKit.readData(getRequest());
            Syslog.info("微信回调消息： " + xmlMsg);
            Map<String, String> map = PaymentKit.xmlToMap(xmlMsg);
            mobileServices.callBackService(map);
            message = "<xml> \n" +
                    "\n" +
                    "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                    "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                    "</xml> \n";
            Syslog.info("回信回调返回消息为 " + message);
        } catch (Exception e) {
            Syslog.error("回调异常");
            Syslog.error(e);
            message = "<xml> \n" +
                    "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                    "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                    "</xml>";
        }

        renderJson(message);
    }

    /**
     * 建议反馈
     */
    public void advicefeedback() {
        Map<String, String[]> map = getParaMap();
        ReturnData w = mobileServices.setAdvicefeedback(map);
        renderJson(w);
    }

    /**
     * 去填写反馈信息
     */
    public void advice() {
        renderJsp("grzxRecord.jsp");
    }

    /**
     * 去注册页面
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void toRegisterPage() {
        renderJsp("loginZhuce.jsp");
    }

    /**
     * 去登陆页面
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void toLoginPage() {
        renderJsp("login.jsp");
    }

    /**
     * 注册
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void register() {
        Map<String, String[]> map = getParaMap();    //userCode和password
        ReturnData rd = mobileServices.register(map);
        renderJson(rd);
    }

    /**
     * 登录
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void login() {

        Map<String, String[]> map = getParaMap();//userCode和password

        ReturnData rd = mobileServices.loginService(map);

        setCookie("type", "7", 60 ^ 60 * 24);

        renderJson(rd);

    }

    /**
     * type用来判断是login登录或者注册register
     * 点击获取按钮生成验证码 6位
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void generateVeriCode() {
        String type = getPara("type");
        String phone = getPara("phone");//@TODO
        ReturnData rd = mobileServices.generateVeriCode(type, phone);
        renderJson(rd);
    }

    /**
     * 去我的页面
     */
    public void toMyInfo() {

        String token = getCookie("token");

        Map map = cache.get(token);

        String type = getCookie("type");

        type = type == null || "".equals(type) ? "1" : type;

        setAttr("member", map.get("memberInfo"));
        Record memberInfo = (Record) map.get("memberInfo");
        String id = memberInfo.getStr("id");
        String realName = Db.queryStr("select realName from yx_member where id =?", id);
        String name = Db.queryStr("select realName from yx_member where id =?", id);
        setAttr("myRealName", realName);
        setAttr("myName", name);
        setAttr("type", type);
        renderJsp("myInfo.jsp");
    }


    /**
     * 我的页面 数据
     */
    public void myInfoData() {

        String token = getHeader("token");

        //Map map = cache.get(token);
        String id=AES.AESDncode(token);

        String sql="select *from yx_member where userId='"+id+"'";

        Record member=Db.findFirst(sql);


        renderJson(member);

    }


    /**
     * 修改密码
     */
    public void updatePwd() {
        String id = getPara("id");
        String password = getPara("oldpwd");
        String newpassword = getPara("newpwd");
        ReturnData returnData = mobileServices.updatepassServices(id, password, newpassword);
        renderJson(returnData);
    }

    /**
     * 去修改密码界面
     */
    public void toUpdatePWDPage() {
        String token = getCookie("token");
        Map<String, Record> map = cache.get(token);
        setAttr("userId", map.get("userInfo").get("id"));
        renderJsp("upPassword.jsp");
    }

    /**
     * 去个人中心页面上的grzxInfo页面
     */
    public void toGrzxInfo() {
        String token = getCookie("token");
        Map<String, Record> map = cache.get(token);
        setAttr("memberInfo", map.get("memberInfo"));
        setAttr("userInfo", map.get("userInfo"));
        renderJsp("grzxInfo.jsp");

    }

    /**
     * 去修改账号页面
     */
    public void toEditAccountPage() {
        String token = getCookie("token");
        Map<String, Record> map = cache.get(token);
        setAttr("memberInfo", map.get("memberInfo"));
        renderJsp("pwdAccount.jsp");
    }

    /**
     * 修改支付宝账号
     */
    public void updateAlipayAccount() {
        Map<String, String[]> map = getParaMap();
        String token = getCookie("token");
        ReturnData rd = mobileServices.updateAlipayAccount(map, token);
        renderJson(rd);
    }

    /**
     * 认证
     */
    @Clear
    public void realName() throws AlipayApiException {
        String token = getCookie("token");
        token=token==null?getHeader("token"):token;
        String name = getPara("name").trim();
        String zhifu = getPara("zhifu").trim();
        String IDCardNumber=getPara("IDCardNumber");
        String mobile = getPara("mobile");
        String captcha = getPara("captcha");

        String cardImg=getPara("cardImg");

        String cardImgReverse=getPara("cardImgReverse");

        ReturnData returnData = null;
        TianyanResult tianyanResult = TianyandataUtil.checkIdCard(name, IDCardNumber);
        if (!tianyanResult.getSuccess()) {
            returnData = ReturnData.fail(tianyanResult.getMsg());
        } else {
            returnData = mobileServices.realNameServices(token, name, zhifu,IDCardNumber,cardImg,cardImgReverse);
            String reditUrl="http://"+PropKit.get("domain").replace("http://","")+"/mobile/zfbRealNamePc"
                    +"?token="+token+"&realName="+name+"&cardId="+IDCardNumber+"&zhifu="+zhifu;
            returnData.setData(reditUrl);
        }

        renderJson(returnData);
    }

    /**
     * 支付宝认证
     */
    public void zfbRealName() throws AlipayApiException {

        Map<String,String[]> map=getParaMap();


        String token = getCookie("token");

        token=token==null?getHeader("token"):token;


        token=token==null?getPara("token"):token;

        String name = map.get("name")[0];



        String cardId=map.get("cardId")[0];




        Map tokenMap = cache.get(token);

        Record memberInfo = (Record) tokenMap.get("memberInfo");



        ReturnData returnData=AliyunBcprovUtil.cprovUtil(name,cardId,token);

        cache.setex(token+"zfbrz",60,returnData.getData());

        cache.setex(token+"zfbrzCack",60,map);

        renderJson(returnData);
    }

    @Clear
    public void zfbRealNameRedict()  {


        String token = getPara("token");

        Syslog.info(token);

        String url=cache.get(token+"zfbrz");

        Syslog.info("*******"+url);

        //redirect(url);

        setAttr("certifyUrl",url);

        renderJsp("zfbRealName.jsp");


    }

    /**
     * 支付宝认证
     */
    @Clear
    public void zfbRealNamePc() throws AlipayApiException {

        Map<String,String[]> map=getParaMap();


        String token=getPara("token");

        Syslog.info("参数   token>>>"+token);


        token=token==null?getHeader("token"):token;


        Syslog.info("heard   token>>>"+token);



        String name = map.get("realName")[0];



        String cardId=map.get("cardId")[0];


        Map tokenMap = cache.get(token);

        Record memberInfo = (Record) tokenMap.get("memberInfo");


        Map<String,String[]> tempMap=new HashMap<>();


        tempMap.put("zhifu",map.get("zhifu"));

        tempMap.put("name",map.get("realName"));


        tempMap.put("cardId",map.get("cardId"));


        Syslog.info("************************");


        cache.setex(token+"zfbrzCack",60,tempMap);

        ReturnData returnData=AliyunBcprovUtil.cprovUtil(name,cardId,token);



        Syslog.info("PC端跳转路径:"+returnData.getData());

        setAttr("certifyUrl",returnData.getData());

        renderJsp("zfbRealName.jsp");


    }


    /**
     * 支付宝实名认证
     */
    @Clear
    public void zfbRealNameCallBack() throws AlipayApiException {


        Syslog.info("支付宝执行了回调");

        String token = getPara("token");

        Map<String,String[]> map=cache.get(token+"zfbrzCack");

        Syslog.info("参数数据为:"+JsonUtil.getJsonStr(map));


        String name = map.get("name")[0];

        String zhifu =  map.get("zhifu")[0];


        String IDCardNumber= map.get("cardId")[0];

        String cardImg="";

        String cardImgReverse="";
        ReturnData returnData = mobileServices.realNameServices(token, name, zhifu,IDCardNumber,cardImg,cardImgReverse);


       renderJsp("succeed.jsp");

    }






    /**
     * 获取返利记录
     */
    public void getRebateLog(){
        String token = getHeader("token");

        Map map = cache.get(token);

        Record memberInfo = (Record) map.get("memberInfo");

        ReturnData returnData=mobileServices.getRebateLog(memberInfo.getStr("userId"));

        renderJson(returnData);
    }

    /**
     * 提现
     */
    public void withdrawDeposit(){
        String amount = getPara("amount");
        String token = getHeader("token");
        Map map = cache.get(token);
        ReturnData returnData= null;
        try {
            returnData = mobileServices.withdrawDeposit(token,map,amount);
        } catch (Exception e) {
            returnData.setResult(true);
            returnData.setMsg("提现失败");
        }
        renderJson(returnData);
    }

    /**
     * 获取提现记录
     */
    public void withdrawLogList(){
        String state = getPara("state");
        String token = getCookie("token");
        token=token==null?getHeader("token"):token;
        Map map = cache.get(token);
        Record memberInfo = (Record) map.get("memberInfo");
        ReturnData returnData= mobileServices.withdrawLogList(memberInfo,state);
        renderJson(returnData);
    }

    /**
     * 去认证页面
     */
    public void realData() {

        String token = getHeader("token");

        Map map = cache.get(token);

        Record memberInfo = (Record) map.get("memberInfo");

        Record member=mobileServices.getMemberByUserIdService(memberInfo.getStr("userId"));

       renderJson(member);

    }


    /**
     * 去认证页面
     */
    @Clear
    public void real() {

        String token = getCookie("token");

        Map map = cache.get(token);

        Record memberInfo = (Record) map.get("memberInfo");

        setAttr("memberInfo", memberInfo);

        renderJsp("realName.jsp");
    }

    /**
     * pc实名认证二维码
     */
    @Clear
    public void pcReal(){


        String token = getPara("token");


        String realName=getPara("realName");

        String payNumber=getPara("payNumber");

        Map map = cache.get(token);

        Record memberInfo = (Record) map.get("memberInfo");

        memberInfo.set("realName",realName);

        memberInfo.set("alipayAccount",payNumber);

        setAttr("memberInfo", memberInfo);

        renderJsp("realName.jsp");


    }

    /**
     * 生成PC端二维码
     */
    @Clear
    public void pcRealQr(){


        String token = getPara("token");

        String realName=getPara("realName");

        String payNumber=getPara("payNumber");

        String cardId=getPara("cardId");

        String domain=PropKit.get("domain");

        String content="http://"+domain.replace("http://","")
                +"/mobile/zfbRealNamePc?token="+token+"&realName="+realName+"&cardId="+cardId
                +"&zhifu="+payNumber;

        renderQrCode(content,400,400);


    }


    /**
     * 闲卡网回调
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void xkCallback() {

        Map<String, String[]> map = getParaMap();

        try {

            Syslog.info("闲卡网回调信息为："+JsonUtil.getJsonStr(MapToRecord.getRecord(map)));
        }catch (Exception e){

            Syslog.error(e);

        }

        Map resultMap = mobileServices.xkCallbackService(map);

        renderJson(resultMap);

    }


    /**
     * 蜜蜂回调
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void mifengCallback() {

        Map<String, String[]> map = getParaMap();

        try {

            Syslog.info("闲卡网回调信息为："+JsonUtil.getJsonStr(MapToRecord.getRecord(map)));
        }catch (Exception e){

            Syslog.error(e);

        }

        String str = mobileServices.mifengCallbackService(map);

        renderText(str);

    }



    /**
     * 收卡网回调
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void skCallback() {

        String str = HttpKit.readData(getRequest());

        Syslog.info(str);

        Map<String, Object> strMap = JsonUtil.getMapFromJsonObjStr(str);

        Syslog.info("收卡啦转换的Map为:" + strMap.toString());

        String result = mobileServices.skCallbackService(strMap);

        renderJson(result);

    }

    /**
     * 易缴费查询状态
     */
    @Clear
    public void yfjCheckState(){

        YJFService.queryCardState();

        renderJson(123);

    }

    /**
     * 易缴费回调
     */
    @Clear
    public void yjfCallback() {

        Syslog.info("易缴费回调");

        Map<String,String[]> map=getParaMap();

        String result = mobileServices.yjfCallbackService(map);

        renderText(result);

    }
    /**
     * 速销卡
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void sxkCallBack(){

        Map<String,String[]> map=getParaMap();

        Record record=MapToRecord.getRecord(map);

        Syslog.info(record.toJson());

        String result=mobileServices.sxkCallBackService(record);

        renderText(result);

    }



    /**
     * 余额统计
     */
    public void balanceStatistical(){
        String token = getCookie("token");

        if (token == null) {
            token = getHeader("token");
        }

        Map<String, Record> map = cache.get(token);

        ReturnData returnData=mobileServices.balanceStatistical(map.get("memberInfo"));
        renderJson(returnData);
    }

    /**
     * 更改手机号
     */

    public void updatePhone(){
        Map<String, String[]> paraMap = getParaMap();
        String token = getCookie("token");

        if (token == null) {
            token = getHeader("token");
        }

        ReturnData returnData=mobileServices.updatePhone(paraMap,token);
        renderJson(returnData);
    }

    /**
     * 公告列表PC端
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void noticeListPc(){
        String page = StringUtils.isNotBlank(getPara("page"))?getPara("page"):"1";
        String size=StringUtils.isNotBlank(getPara("size"))?getPara("size"):"6";
        String type = getPara("type");
        ReturnData returnData=mobileServices.noticeListPc(page,size,type);
        renderJson(returnData);
    }

    /**
     * 转账记录
     */
    public void accountLog(){
        Map<String, String[]> map = getParaMap();
        String token=getCookie("token");
        if (token==null){
            token=getHeader("token");
        }
        ReturnData returnData=mobileServices.accountLog(token,map);
        renderJson(returnData);
    }

    /**
     * 热门卡项列表
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void hotCommodityList(){
        String type = getPara("type");
        List<Record> list = mobileServices.hotCommodityList(type);
        ReturnData returnData=new ReturnData();
        if (list!=null&&list.size()>0){
            returnData.setResult(true);
            returnData.setData(list);
        }else{
            returnData.setResult(false);
            returnData.setMsg("暂无数据");
        }
        renderJson(returnData);
    }

    /**
     * 单个图片上传
     */
    @Clear
    @Before(CacheInterceptor.class)
    public void imgUpload() {



        UploadFile file = getFile();

//        type = file.getContentType().equals(type) == false ? "jpg" : type;
//
//        String path = PropKit.get("rootPath") + "/upload/" + UtilUUID.getUUID() + "." + type;
//
//        file.getFile().renameTo(new File(path));

        String imgUrl = QiNiuUploadUtil.localFileUpload( file.getUploadPath()+"/"+ file.getOriginalFileName());

        Map map = new HashMap();

        map.put("code", "0");
        map.put("msg", "上传成功");
        Map data = new HashMap();
        data.put("src", imgUrl);
        data.put("title", "");
        map.put("data", data);
        renderJson(map);
    }

    /**
     * app 下载
     */
    @Clear
    public void appDown(){

        String downPath= PropKit.get("appFilePath");

        File file=new File(downPath);

        renderFile(file);

    }



}